//
//  SleepDataBarChartView.swift
//  MoyoungHome
//
//  Created by 李然 on 2022/9/26.
//

import UIKit
import Foundation

import RxSwift
import RxCocoa
import MYModule


open class SleepDataBarChartView: View {
    
   
    public var lineTipTypes: [LineTipType] = [] {
        
        didSet {
            
            if lineTipTypes == oldValue {
                return
            }
            
            lineTipStack.arrangedSubviews.forEach {
                lineTipStack.removeArrangedSubview($0)
            }
           
            for type in lineTipTypes {
                let lineTipView = SleepLineTipView(color: type.tintColor, title: type.title)
                lineTipStack.addArrangedSubview(lineTipView)
            }
            
        }
        
    }
    
    let lineTipStack = StackView()
    
    
    open override func makeUI() {
        super.makeUI()
        
        self.backgroundColor = UIColor.background
        
        let dataStack = StackView()
        dataStack.axis = .horizontal
        dataStack.alignment = .firstBaseline
        dataStack.contentMode = .center
        
        dataStack.addArrangedSubview(hourLabel)
        dataStack.addArrangedSubview(hourUnitLabel)
        dataStack.addArrangedSubview(minuteLabel)
        dataStack.addArrangedSubview(minuteUnitLabel)
        
        let labelStackView = StackView()
        labelStackView.axis = .vertical
        labelStackView.alignment = .center
        
        self.addSubview(labelStackView)
        labelStackView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.centerX.equalToSuperview()
        }
        
        labelStackView.addArrangedSubview(dataStack)
        labelStackView.addArrangedSubview(timeLabel)
        
       
        lineTipStack.axis = .horizontal
        lineTipStack.alignment = .center
        lineTipStack.spacing = 30
        
        
        for type in lineTipTypes {
            let lineTipView = SleepLineTipView(color: type.tintColor, title: type.title)
            lineTipStack.addArrangedSubview(lineTipView)
        }
    
        self.addSubview(lineTipStack)
        
        lineTipStack.snp.makeConstraints { make in
            make.top.equalTo(labelStackView.snp.bottom).offset(26)
            make.centerX.equalToSuperview()
        }
        
        self.addSubview(barChartView)
        barChartView.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-20)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalTo(170)
        }
    }
    
    func showDefaultData() {
        
       
        
    }
    
    func bind(by viewModel:SleepDataChartViewModel){
        let input = SleepDataChartViewModel.Input(selectIndex: self.barChartView.selectIndex)
        let output = viewModel.transform(input: input)
        output.timeString.drive(self.timeLabel.rx.text).disposed(by: rx.disposeBag)
        output.hourString.drive(self.hourLabel.rx.text).disposed(by: rx.disposeBag)
        output.minuteString.drive(self.minuteLabel.rx.text).disposed(by: rx.disposeBag)
        
        switch viewModel.chartType {
            
        case .day:
            
            lineTipTypes = LineTipType.allCases
            break
        case .week:
            lineTipTypes = LineTipType.allCases.filter({ $0 != .awake })
            break
        case .month:
            lineTipTypes = LineTipType.allCases.filter({ $0 != .awake })
            break
        case .custom:
            break
        }
        
        
        self.barChartView.setBarCount(viewModel.getBarCount())
        self.barChartView.setXAxis(valueFormatter: viewModel.getXAxisValueFotmatter())
        
        let colors: [UIColor] = [
        
            UIColor.Sleep.restful,
            UIColor.Sleep.light,
            UIColor.Sleep.rem,
            
        ]
        
        var colorIndex = 0
        
        let barDataLists = viewModel.dataSource.enumerated().map {(i, record) -> DFBarDataModel in
            let deep = Double(record.deep == 0 ? Int(viewModel.YMin) : record.deep)
            let light = Double(record.light)
            let rem = Double(record.rem)
            
            
            // [deep, light, rem]
            var model = DFBarDataModel(colors: colors, datas: [deep, light, rem])
            model.hightColor = UIColor.clear.withAlphaComponent(0.1)
            colorIndex = colorIndex + 1
            if colorIndex >= colors.count {
                colorIndex = 0
            }
            return model
        }
        
        self.barChartView.setYMax(value: viewModel.YMax)
        
        self.barChartView.setBarDataModels(barDataLists, highlightEnabled: true)
        
        
    }
    
    func setChartConfig(_ config:DFBarChartViewConfig){
        self.barChartView.setConfig(config)
    }
    
    func slider(_ point:CGPoint){
        self.barChartView.setTouchLocation(point)
    }
    
    public func clearHighlight() {
        self.barChartView.highlightValue(nil)
    }
    
    
    lazy var hourLabel: Label = {
        let label = Label.init()
        label.font = Fonts.boldSystemFont(ofSize: 40)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    lazy var hourUnitLabel: Label = {
        let label = Label.init()
        label.font = Fonts.systemFont(ofSize: 11)
        label.textColor = UIColor.assist_222222
        label.text = "sleep_hour".language
        return label
    }()
    
    lazy var minuteLabel: Label = {
        let label = Label.init()
        label.font = Fonts.boldSystemFont(ofSize: 40)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    lazy var minuteUnitLabel: Label = {
        let label = Label.init()
        label.font = Fonts.systemFont(ofSize: 11)
        label.textColor = UIColor.assist_222222
        label.text = "sleep_min".language
        return label
    }()
    
    lazy var timeLabel: Label = {
        let label = Label.init()
        label.font = Fonts.systemFont(ofSize: 11)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
   public lazy var barChartView: DFBarChartView = {
       let view = DFBarChartView.init(frame: .zero)
        return view
    }()
  
}

extension SleepDataBarChartView {
    
    public enum LineTipType: CaseIterable {
         case restful
         case light
         case REM
         case awake
        
        
        var tintColor: UIColor {
            
            switch self {
                
            case .restful:
                return UIColor.Sleep.restful
            case .light:
                return UIColor.Sleep.light
            case .REM:
                return UIColor.Sleep.rem
            case .awake:
                return UIColor.Sleep.awake
            }
            
        }
        
        var title: String {
            
            switch self {
                
            case .restful:
                return "sleep_restful".language
            case .light:
                return "sleep_light".language
            case .REM:
                return "sleep_eye_movement".language
            case .awake:
                return "sleep_awake".language
            }
        }
     }
    
    
    
}



class SleepLineTipView: StackView {
    
    init(color:UIColor,title:String) {
        super.init(frame: .zero)
        
        self.axis = .horizontal
        self.alignment = .center
        self.spacing = 5
        
        let circleView = View()
        circleView.backgroundColor = color
        circleView.cornerRadius = 5
        addArrangedSubview(circleView)
        circleView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 10, height: 10))
        }
        
        let label = Label()
        label.text = title
        label.textColor = UIColor.assist_222222
//        label.numberOfLines = 2
        label.font = Fonts.systemFont(ofSize: 11)
        addArrangedSubview(label)
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
